/*
 *  WilNoteTip.cpp
 *  Wilson
 *
 *  Created by Sebastian Orfino on 1/25/12.
 *  Copyright 2012 lbs3. All rights reserved.
 *
 */


#include "VCPlugInHeaders.h"

// Interface includes:
#include "IActiveContext.h"
#include "IControlView.h"
#include "ITip.h"
#include "IWidgetParent.h"
#include "IDataBase.h"
#include "IWorkspace.h"
#include "UIDNodeID.h"
#include "IDocument.h"
#include "StringUtils.h"

//project includes
#include "WilID.h"
#include "xmp_facade.h"
#include "IWilNoteData.h"
#include "adobe/unicode.hpp"


class WilNoteTip  : public CPMUnknown<ITip>
{			
public:
	WilNoteTip( IPMUnknown *boss );
	virtual ~WilNoteTip();
	
	virtual PMString  GetTipText(const PMPoint& mouseLocation,ITip::TipWindowPlacementAdvice *outWindowPlacement);
};

CREATE_PMINTERFACE( WilNoteTip, kWilNoteTipImpl )


/* Constructor
 */
WilNoteTip::WilNoteTip(IPMUnknown *boss):
CPMUnknown<ITip>(boss)
{
}

/* Destructor
 */
WilNoteTip::~WilNoteTip()
{
}

/* GetTipText 
 */
PMString WilNoteTip::GetTipText(const PMPoint& mouseLocation,ITip::TipWindowPlacementAdvice *outWindowPlacement)
{
	using namespace std;
	using namespace xmp;
	PMString activeTip = "";
	
	do {
	
		InterfacePtr<IWilNoteData> noteData(this,UseDefaultIID());
		BREAK_IF(!noteData);
		
		IDataBase * db = ::GetDataBase(this);
		BREAK_IF(!db);
		
		InterfacePtr<IDocument> doc(db,db->GetRootUID(),UseDefaultIID());
		BREAK_IF(!doc);
		
		xmp::info_nota_t mi_nota = { noteData->uid().str() };
		xmp::wilson_metadata metadata(::GetUIDRef(doc));
		vector<info_comentario_t> coms = metadata.listar_comentarios(mi_nota);
		if (coms.size())
		{
			info_comentario_t com = coms.back();
			WideString aux; //por el momento no encuentro forma de convertir mi std::string en UTF8 a PMString sin hacer lo que sigue.
			//quizas la mejor opcion sea de una vez por todas cambiar todos los miembros de xmp:: a PMString y generar un operator << copado.
			StringUtils::ConvertUTF8ToWideString(com.texto, aux);
			activeTip = aux; 
		}
		
	} while(false);
	
	return activeTip;
	/*do
	{	
		InterfacePtr<IControlView> controlView(this, UseDefaultIID());
		if (controlView == nil)
		{
			ASSERT_FAIL("IControlView is invalid");
			return "";
		}
		
		WidgetID widgetID = controlView->GetWidgetID();
		if (widgetID == nil)
		{
			ASSERT_FAIL("WidgetID is invalid");
			return "";
		}
		
		InterfacePtr<IWidgetParent> iWidgetParent(this, UseDefaultIID());
		if (iWidgetParent == nil)
		{
			ASSERT_FAIL("IWidgetParent is invalid");
			break;
		}
		InterfacePtr<ITreeNodeIDData> holderData(iWidgetParent->GetParent(), UseDefaultIID());
		if (holderData == nil)
		{
			ASSERT_FAIL("ITreeNodeIDData is invalid");
			break;
		}
		IActiveContext *context = GetExecutionContextSession()->GetActiveContext();
		if (context == nil)
		{
			ASSERT_FAIL("IActiveContext is invalid");
			break;
		}
		IWorkspace *ws = context->GetContextWorkspace();
		if (ws == nil)
		{
			ASSERT_FAIL("IWorkspace is invalid");
			break;
		}
		IDataBase* db = ::GetDataBase(ws);
		if (db == nil)
		{
			ASSERT_FAIL("IDataBase is invalid");
			break;
		}
		
		TreeNodePtr<UIDNodeID>    uidNode(holderData->Get());
		if (uidNode == nil)
		{
			ASSERT_FAIL("UIDNodeID is invalid");
			break;
		}
		
		InterfacePtr<ICustomConditionTag> customConditionTag( ::GetDataBase(ws), uidNode->GetUID(), UseDefaultIID());		
		switch(widgetID.Get())
		{
			case kConditionTagIsPrintedStateWidgetId:
				if(customConditionTag->IsPrintVisible())
				{
					activeTip = kCusCondTxtUIPrintedIconVisibleStringKey;
				}
				else
				{
					activeTip = kCusCondTxtUIPrintedIconInvisibleStringKey;
				}
				break;
				
			case kConditionTagIsIdmlStateWidgetId:
				if(customConditionTag->IsIdmlVisible())
				{
					activeTip = kCusCondTxtUIIdmlIconVisibleStringKey;
				}
				else
				{
					activeTip = kCusCondTxtUIIdmlIconInvisibleStringKey;
				}	
				break;
				
			default:
				activeTip = "";
		}			
		
	} while (false);
	
	return activeTip;*/
} 